//接口配置
var api_cfg = {
	"domain": "./qingke/api/",
	"verify": "./qingke/api/verify",
	"file_upload": "file_upload",
	"file_domain": "../src/",
	"login": "login",
	"pwd": "pwd",
	"sys_menu": "sys_menu",
	"menu_detail": "menu_detail",
	"gruop_menu": "gruop_menu",
	"set_menu": "set_menu",
	"menu_status": "menu_status",
	"menu_dele": "menu_dele",
	"menu_sort": "menu_sort",
	"menu_edit": "menu_edit",
	"user_list": "user_list",
	"user_edit": "user_edit",
	"user_dele": "user_dele",
	"user_status": "user_status",
	"user_detail": "user_detail",
	"group_list": "group_list",
	"group_status": "group_status",
	"group_dele": "group_dele",
	"group_detail": "group_detail",
	"gruop_edit": "gruop_edit",
	"program_list": "program_list",
	"program_del": "program_del",
	"program_status": "program_status",
	"program_info": "program_info",
	"program_edit": "program_edit",
	"image": "image",
	"lesson_list": "lesson_list",
	"lesson_info": "lesson_info",
	"lesson_comments": "lesson_comments",
	"lesson_edit": "lesson_edit",
	"lesson_del": "lesson_del",
	"lesson_status": "lesson_status",
	"promotion": "promotion",
	"promotion_status": "promotion_status",
	"lesson_del": "lesson_del",
	"promotion_edit": "promotion_edit",
	"promotion_image": "promotion_image",
	"category_list": "category_list",
	"category_status": "category_status",
	"category_edit": "category_edit",
	"category_image": "category_image",
	"category_del": "category_del",
	"sort": "sort",
	"category_program": "category_program",
	"program_sort": "program_sort",
	"category_program_del": "category_program_del",
	"comic_add": "comic_add",
	"category_lesson": "category_lesson",
	"category_lesson_del": "category_lesson_del",
	"lesson_sort": "lesson_sort",
	"parent_category": "parent_category",
	"child_category": "child_category",
	"category_lesson_add": "category_lesson_add",
	"related_add": "related_add",
	"related_sort": "related_sort",
	"lesson_comments": "lesson_comments",
	"comments_sort": "comments_sort",
	"comments_add": "comments_add",
	"group_edit":"group_edit",
	"lesson_related":"lesson_related"
};
//导航菜单配置
var menu_cfg = [];
var program_type = {
	"1": "单片",
	"2": "剧头",
	"3": "分集"
};
var promotion_type = {
	"1": "视频",
	"2": "课程",
	"3": "栏目",
	"4": "专题"
};
var promotion_grade = {
	"-1": "通用",
	"1": "一年级",
	"2": "二年级",
	"3": "三年级",
	"4": "四年级",
	"5": "五年级",
	"6": "六年级"
}

/**
 * 下列配置或方法，修改影响布局
 */
var newModal = "<div class=\"modal fade\" id=\"newModal\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"newModalLabel\"><div class=\"modal-dialog modal-lg\" role=\"document\"><div class=\"modal-content\"><div class=\"modal-header\"><button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button><h4 class=\"modal-title\" id=\"newModalLabel\"></h4></div><div class=\"modal-body\"><iframe id=\"newiframe\" width=\"100%\" height=\"450px\" frameborder=\"0\"></iframe></div></div></div></div>";
//$(".container").filter(":last").after(newModal);
var api = {
	"_global": {
		"_pagination": {
			"page": 1,
			"count": 20
		}
	}
};
$.ajaxSetup({
	type: 'POST',
	dataType: 'JSON',
	async: true,
	cache: false,
	beforeSend: function(request) {
		request.setRequestHeader("Authorization", localStorage.getItem("Authorization"));
	},
	complete: function(xhr, status) {
		localStorage.setItem("Authorization", xhr.getResponseHeader('Authorization'));
		var resp = xhr.responseText;
		var j = JSON.parse(resp);
		if(j.hasOwnProperty("err") && j.err == -2) {
			msgErr("重新登录！");
			exit();
			return false;
		}
	}
});

function getBrowserInfo() {
	var Sys = {};
	var ua = navigator.userAgent.toLowerCase();
	var re = /(msie|firefox|chrome|opera|version).*?([\d.]+)/;
	var m = ua.match(re);
	Sys.browser = m[1].replace(/version/, "safari");
	Sys.ver = m[2];
	return Sys.browser;
}

function checkStrong(pwd) {
	var modes = 0;
	if(pwd.length < 6) return 0;
	if(/\d/.test(pwd)) modes++; //数字
	if(/[a-z]/.test(pwd)) modes++; //小写
	if(/[A-Z]/.test(pwd)) modes++; //大写
	if(/\W/.test(pwd)) modes++; //特殊字符
	if(pwd.length > 12) return 4;
	return modes < 2 ? false : true; //1弱 2中 3强 4牛
}

function reLoad() {
	var url = window.location.href;
	var parm = parseInt(Math.random() * 10);
	if(url.lastIndexOf('?') > -1) {
		url = url + "&" + parm;
	} else {
		url = url + "?" + parm;
	}
	window.location.href = url;
}

function urlAppend(url, param) {
	if(url.lastIndexOf('?') > -1) {
		url = url + "&" + param;
	} else {
		url = url + "?" + param;
	}
	return url;
}

function auth() {
	var Authorization = localStorage.getItem("Authorization");
	if(Authorization == undefined || Authorization == "" || Authorization == "null") {
		exit();
	}
}

function msg(info) {
	parent.msg(info);
}

function msgErr(info) {
	parent.msgErr(info);
}

function goto(target) {
	$("#main", parent.document).attr("src", target + ".html");
}

function load() {
	goto("portal");
}

function exit() {
	localStorage.removeItem("Authorization");
	localStorage.clear();
	window.top.location.reload();
}
Date.prototype.format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1,
		"d+": this.getDate(),
		"h+": this.getHours(),
		"m+": this.getMinutes(),
		"s+": this.getSeconds(),
		"q+": Math.floor((this.getMonth() + 3) / 3),
		"S": this.getMilliseconds()
	};
	if(/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

function friendlyMoney(num) {
	num = parseFloat(parseInt(num) / 100);
	s = num.toString();
	if(/[^0-9\.]/.test(s)) return "invalid value";
	s = s.replace(/^(\d*)$/, "$1.");
	s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
	s = s.replace(".", ",");
	var re = /(\d)(\d{3},)/;
	while(re.test(s))
		s = s.replace(re, "$1,$2");
	s = s.replace(/,(\d\d)$/, ".$1");
	return "￥" + s.replace(/^\./, "0.")
}

function friendlySec(time) {
	time = parseInt(time);
	var hh, mm, ss;
	if(time == null || time < 0) {
		return;
	}
	hh = time / 3600 | 0;
	time = parseInt(time) - hh * 3600;
	if(parseInt(hh) < 10) {
		hh = "0" + hh;
	}
	mm = time / 60 | 0;
	ss = parseInt(time) - mm * 60;
	if(parseInt(mm) < 10) {
		mm = "0" + mm;
	}
	if(ss < 10) {
		ss = "0" + ss;
	}
	return hh + ":" + mm + ":" + ss;
}

function toSec(time) {
	var t = time.split(":");
	return parseInt(t[0]) * 3600 + parseInt(t[1]) * 60 + parseInt(t[2]);
}

function menu(target) {
	if(sessionStorage["sys_login"] == undefined){
		exit();
	}
	var Authorization = localStorage["Authorization"];
	if(Authorization != undefined && Authorization != "" && Authorization != "null"){
		$.ajax({
			type: "post",
			url: api_cfg.domain + api_cfg.sys_menu,
			async: false,
			data: {
				id: localStorage["login-uid"]
			},
			success: function(data) {
				var t = JSON.stringify(data).indexOf('"param":"'+target);
				if(!(target == undefined || t >-1)){
					goto("portal");
					window.top.location.href = "/tpl";
				}
				menu_cfg = data;
			}
		});
	}
	var name = localStorage.getItem("login-name");
	var menu_html = "<div class=\"navbar-header\"><button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\"> <span class=\"sr-only\">Toggle navigation</span> <span class=\"icon-bar\"></span> <span class=\"icon-bar\"></span> <span class=\"icon-bar\"></span> </button> <a class=\"navbar-brand\" href=\"javascript:load();\">CMS</a></div>";
	menu_html += "<div id=\"navbar\" class=\"navbar-collapse collapse\">";
	menu_html += "<ul class=\"nav navbar-nav\">";
	menu_cfg.forEach(function(val, i, item) {
		if(val.hasOwnProperty("sub")) {
			var parent = "<li class=\"dropdown\">";
			var parent_is = "",
				parent_sub = "";
			val.sub.forEach(function(s_val, s_i, s_item) {
				parent_is = s_val.param == target ? " class=\"active\"" : "";
				parent_sub += "<li" + parent_is + "><a href=\"javascript:goto('" + s_val.param + "');\">" + s_val.title + "</a></li>";
				if(parent_is != "") {
					parent = "<li class=\"dropdown active\">";
				}
			});
			parent += "<a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">" + val.title + "<span class=\"caret\"></span></a>";
			parent += "<ul class=\"dropdown-menu\">";
			parent += parent_sub;
			menu_html += parent + "</ul></li>";
		} else {
			var is = val.param == target ? " class=\"active\"" : "";
			menu_html += "<li" + is + "><a href=\"javascript:goto('" + val.param + "');\">" + val.title + "</a></li>";
		}
	});
	menu_html += "</ul>";
	menu_html += "<ul class=\"nav navbar-nav navbar-right\"> <li class=\"dropdown\"> <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">" + name + "<span class=\"caret\"></span></a> <ul class=\"dropdown-menu\"> <li><a href=\"pwd.html\">修改密码</a></li> <li role=\"separator\" class=\"divider\"></li> <li><a href=\"javascript:exit();\">注销</a></li> </ul> </li></ul>";
	menu_html += "</div>";
	if(api_cfg.hasOwnProperty("logo") && api_cfg.logo.length > 0) {
		menu_html = "<div style=\"display: block;float: left;padding: 10px;margin-left: -100px\"><img src=\"" + api_cfg.logo + "\" height=\"30px\"></div>" + menu_html;
	}
	$(".navbar").html("<div class=\"container\">" + menu_html + "</div>");
}

function pagination(url, page, total) {
	$(".pagination").html("");
	page = parseInt(page);
	total = parseInt(total);
	if(total < 2) {
		return false;
	}
	url = urlAppend(url, "page=");
	var f = "";
	if(page == 1) {
		f += "<li class=\"disabled\"><span>&laquo;</span></li> ";
	} else {
		var pre = page - 1;
		f += "<li><a href=\"" + url + pre + "\">&laquo;</a></li> ";
	}
	var h = "";
	var roll = 10;
	var c = roll / 2;
	var ce = Math.ceil(c);
	var k, s = 0,
		e;
	for(var i = 1; i <= roll; i++) {
		if((page - c) <= 0) {
			k = i;
		} else if((page + c - 1) >= total) {
			k = total - roll + i;
		} else {
			k = page - ce + i;
		}
		if(k > 0 && k != page) {
			if(k <= total) {
				h += "<li><a href=\"" + url + k + "\">" + k + "</a></li>";
				s = s != 0 ? s : k;
				e = k;
			} else {
				break;
			}
		} else {
			if(k > 0 && total != 1) {
				h += "<li class=\"active\"><span>" + k + "</span></li>";
				s = s != 0 ? s : k;
				e = k;
			}
		}
	}
	if(s != 1) {
		f = f + "<li><a href=\"" + url + "1\">P1</a></li>";
	}
	if(e != total) {
		h += "<li><a href=\"" + url + total + "\">P" + total + "</a></li>";
	}
	if(page == total) {
		h += "<li class=\"disabled\"><span>&raquo;</span></li>";
	} else {
		var next = page + 1;
		h += "<li><a href=\"" + url + next + "\">&raquo;</a></li>";
	}
	$(".pagination").html(f + h);
}

function aEdit(whatever, rowid) {
	return "<button type=\"button\" class=\"btn btn-default\" data-toggle=\"modal\" data-target=\"#newModal\" data-ext=\"edit\" data-whatever=\"" + whatever + "\" data-rowid=\"" + rowid + "\"><i class=\"fa fa-pencil fa-lg\"></i></button>";
}

function aImage(whatever, rowid, img) {
	return "<button type=\"button\" class=\"btn btn-default\" data-toggle=\"modal\" data-target=\"#newModal\" data-ext=\"add-img\" data-whatever=\"" + whatever + "\" data-rowid=\"" + rowid + "\" data-img=\"" + img + "\"><i class=\"fa fa-image fa-lg\"></i></button>";
}

function aStatus(action, id, status) {
	var fa = (status == 1) ? "on" : "off";
	return "<button type=\"button\" class=\"btn btn-status btn-default\" data-action=\"" + action + "\" data-id=\"" + id + "\" data-status=\"" + status + "\" onclick=\"btnStatus(this)\"><i class=\"fa fa-toggle-" + fa + " fa-lg\"></i></button>";
}

function btnStatus(e) {
	var action = $(e).attr("data-action");
	var id = $(e).attr("data-id");
	var status = $(e).attr("data-status");
	status = (parseInt(status) == 1) ? 0 : 1;
	$.post(api_cfg.domain + action, {
		id: id,
		status: status
	}, function(data) {
		if(data.err == 0) {
			$(e).attr("data-status", status);
			var fa = (status == 1) ? "on" : "off";
			$(e).html("<i class=\"fa fa-toggle-" + fa + " fa-lg\"></i>");
		} else {
			msgErr("error");
		}
	}, 'json');
}

function aDele(action, id, row) {
	return "<button type=\"button\" class=\"btn btn-warning\" data-action=\"" + action + "\" data-id=\"" + id + "\" data-rowid=\"" + row + "\" onclick=\"btnDele(this)\"><i class=\"fa fa-trash fa-lg\"></i></button>";
}

function btnDele(e) {
	var row = $(e).attr("data-rowid");
	if(confirm("#" + row + " will be delete?")) {
		var action = $(e).attr("data-action");
		var id = $(e).attr("data-id");
		$.post(api_cfg.domain + action, {
			id: id
		}, function(data) {
			if(data.err == 0) {
				$("#tr_" + id).hide();
			} else {
				msgErr("error");
			}
		}, 'json');
	}
}

function aPromotion(action, id, status) {
	var fa = (status == 1) ? "primary" : "default";
	var txt = (status == 1) ? "取消推荐" : "设置推荐";
	return "<button type=\"button\" class=\"btn btn-" + fa + "\" data-action=\"" + action + "\" data-id=\"" + id + "\" data-status=\"" + status + "\" onclick=\"btnPromotion(this)\">" + txt + "</button>";
}

function btnPromotion(e) {
	var action = $(e).attr("data-action");
	var id = $(e).attr("data-id");
	var status = $(e).attr("data-status");
	status = (parseInt(status) == 1) ? 0 : 1;
	$.post(api_cfg.domain + action, {
		id: id,
		promotion: status
	}, function(data) {
		if(data.err == 0) {
			$(e).attr("data-status", status);
			var fa = (status == 1) ? "primary" : "default";
			var txt = (status == 1) ? "取消推荐" : "设置推荐";
			$(e).removeClass().addClass("btn btn-" + fa)
			$(e).html(txt);
		} else {
			msgErr("error");
		}
	}, 'json');
}

api.allmenu = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.sys_menu, params, function(data) {
		var h = "",
			row = (params.page - 1) * params.count + 1;
		data.forEach(function(val, i, item) {
			h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
			h += "<td>" + val.title + "</td>";
			var param = val.param ? val.param : "";
			h += "<td class='col-xs-3 col-lg-2'>" + param + "</td>";
			h += "<td class='col-xs-2 col-lg-1'><input class='form-control' name='ids[" + val.id + "]' value='" + val.sort_num + "' ></td>";
			h += "<td><div class=\"btn-group\" role=\"group\">";
			h += aEdit(val.id, row);
			h += aStatus("menu_status", val.id, val.status);
			h += aDele("menu_dele", val.id, row);
			h += "</div></td>";
			h += "</tr>";
			row++;
			if(val.hasOwnProperty("sub")) {
				val.sub.forEach(function(val) {
					h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
					h += "<td>" + "---" + val.title + "</td>";
					var param = val.param ? val.param : "";
					h += "<td class='col-xs-3 col-lg-2'>" + param + "</td>";
					h += "<td class='col-xs-2 col-lg-1'><input class='form-control' name='ids[" + val.id + "]' value='" + val.sort_num + "' ></td>";
					h += "<td><div class=\"btn-group\" role=\"group\">";
					h += aEdit(val.id, row);
					h += aStatus("menu_status", val.id, val.status);
					h += aDele("menu_dele", val.id, row);
					h += "</div></td>";
					h += "</tr>";
					row++;
				});
			}
		});
		$("#table-body").html(h);
		var url = "menu.html";
	}, 'json');
};
api.parentMenu = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.sys_menu, params, function(data) {
		var h = '<option value="0">---</option>';
		data.forEach(function(val) {
			h += '<option value="' + val.id + '">' + val.title + '</option>';
		});
		$("#parent").html(h);
	});
};
api.menuDetail = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.menu_detail, params, function(data) {
		if(data.err == 0) {
			$("#status").val(data.body.status);
			$("#parent").val(data.body.parent_id);
			$("#title").val(data.body.title);
			$("#param").val(data.body.param);
		}
	});
};
api.menuEdit = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.menu_edit, params, function(data) {
		if(data.err == 0) {
			parent.reLoad();
		} else {
			msgErr("失败");
		}
	});
};
api.menuSort = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.menu_sort, opts, function(data) {
		if(data.err == 0) {
			reLoad();
		} else {
			msgErr("失败");
		}
	});
};
api.login = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.login, params, function(data) {
		if(data.err == 0) {
			localStorage.setItem("login-uid", data["body"]["uid"]);
			localStorage.setItem("login-name", data["body"]["name"]);
			sessionStorage["sys_login"] = new Date().getTime();
			msg("welcome");
			window.location.href = "portal.html";
		} else if(data.err == 3) {
			$(".verify-code").attr("src", urlAppend(api_cfg.verify, Math.random()));
			$("#code").val("");
			msgErr("验证码错误");
		} else {
			$(".verify-code").attr("src", urlAppend(api_cfg.verify, Math.random()));
			$("#code").val("");
			msgErr("账号或密码错误");
		}
	}, 'json');
};

api.pwd = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.pwd, params, function(data) {
		if(data.err == 0) {
			$("#pwd1").val("");
			$("#pwd2").val("");
			$("#pwd3").val("");
			msg("设置成功！");
			exit();
		} else if(data.err == -1) {
			msgErr("密码错误");
		}
	}, 'json');
};

api.userList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.user_list, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + val.account + "</td>";
				h += "<td>" + val.group + "</td>";
				var time = new Date(val.utime);
				h += "<td>" + val.ctime + "<br/>" + val.utime + "</td>";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.id, row);
				h += aStatus(api_cfg.user_status, val.id, val.status);
				h += aDele(api_cfg.user_dele, val.id, row);
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "user.html";
			if(params.hasOwnProperty("status")) {
				url = urlAppend(url, "status=" + params.status);
			}
			if(params.hasOwnProperty("account")) {
				url = urlAppend(url, "account=" + params.account);
			}
			if(params.hasOwnProperty("group")) {
				url = urlAppend(url, "gruop=" + params.group);
			}
			pagination(url, params.page, data.body.total);
		}else if(data.err == 2) {
			msgErr("没有符合条件的数据");
		} else {
			msgErr("error");
		}
	}, 'json');
};
api.userDetail = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.user_detail, params, function(data) {
		if(data.err == 0) {
			$("#account").val(data.body.account);
			$("#group").val(data.body.group_id);
			$("#status").val(data.body.status);
		} else {
			msg("error");
		}
	}, 'json');
};
api.userEdit = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.user_edit, params, function(data) {
		if(data.err == 0) {
			if(params.id > 0) {
				msg("更新一条记录！");
			} else {
				msg("新增一条记录！");
			}
			parent.reLoad();
		} else if(data.err == 5) {
			msg("账号已存在");
		} else {
			msg("error");
		}
	}, 'json');
};

api.groupList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.group_list, params, function(data) {
		if(data.err == 0) {
			var h = "",row = (params.page - 1) * params.count + 1;
			data.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + val.name + "</td>";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.id, row);
				h += aStatus("group_status", val.id, val.status);
				h += aDele("group_dele", val.id, row);
				h += "<button type=\"button\" class=\"btn btn-default\" data-toggle=\"modal\" data-target=\"#newModal\" data-ext=\"group\" data-whatever=\"" + val.id + "\" data-rowid=\"" + row + "\"><i class=\"fa fa-chain fa-lg\"></i></button>";
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
		} else if(data.err == 2) {
			msgErr("没有符合条件的数据");
		} else {
			msgErr("error");
		}
	}, 'json');
};

api.groupDetail = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.group_detail, params, function(data) {
		if(data.err == 0) {
			$("#name").val(data.body.name);
			$("#status").val(data.body.status);
		} else {
			msg("error");
		}
	}, 'json');
};

api.groupEdit = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.group_edit, params, function(data) {
		if(data.err == 0) {
			if(params.id > 0) {
				msg("更新一条记录！");
			} else {
				msg("新增一条记录！");
			}
			parent.reLoad();
		} else {
			msg("error");
		}
	}, 'json');
};

api.menuList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.sys_menu, params, function(data) {
		var h = "";
		data.forEach(function(val) {
			h += '<div class="panel-group"><div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title">';
			var check = "";
			if(mylist.indexOf(val.id) > -1) {
				check = "checked"
			}
			h += '<input name=ids[] value="' + val.id + '" type="checkbox" ' + check + ' />';
			h += '<a data-toggle="collapse" data-parent="#accordion" aria-expanded="true" href="#p' + val.id + '">' + val.title + '</a></h4></div>';
			if(val.hasOwnProperty("sub")) {
				h += '<div id="p' + val.id + '" aria-expanded="true" class="panel-collapse collapse in"><div class="panel-body">';
				val.sub.forEach(function(ls) {
					var checkt = ""
					if(mylist.indexOf(ls.id) > -1) {
						checkt = "checked"
					}
					h += '<p>-- <input name=ids[] value="' + ls.id + '" type="checkbox" ' + checkt + '/>' + ls.title + '</p>';
				});
				h += '</div></div>';
			}
			h += '</div>';
		});
		$("#grouplist").html(h);
	}, 'json');
};

//api.productList = function(opts) {
//	var params = $.extend({}, api._global._pagination, opts);
//	$.post(api_cfg.domain + api_cfg.product_list, params, function(data) {
//		if(data.err == 0) {
//			var h = "",
//				row = (params.page - 1) * params.count + 1;
//			data.body.list.forEach(function(val, i, item) {
//				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
//				h += "<td>" + val.code + "</td>";
//				h += "<td>" + val.name + "</td>";
//				var qr = "";
//				if(val.hasOwnProperty("qr_code") && val.qr_code != "" && val.qr_code != null) {
//					qr = val.qr_code;
//					h += "<td><a href=\"" + api_cfg.file_domain + val.qr_code + "\" target=\"_blank\"><img src=\"" + api_cfg.file_domain + val.qr_code + "\" class=\"img-thumbnail\"></a></td>";
//				} else {
//					h += "<td></td>";
//				}
//				h += "<td>" + friendlySec(val.video_length) + "</td>";
//				h += "<td>" + friendlyMoney(val.price) + "</td>";
//				h += "<td>" + friendlyMoney(val.sales) + "</td>";
//				h += "<td><div class=\"btn-group\" role=\"group\">";
//				h += aEdit(val.id, row);
//				h += aImage(val.id, row, qr);
//				h += aPromotion(api_cfg.product_promotion, val.id, val.promotion);
//				h += aStatus(api_cfg.product_status, val.id, val.status);
//				h += aDele("program_del", val.id, row);
//				h += "</div></td>";
//				h += "</tr>";
//				row++;
//			});
//			$("#table-body").html(h);
//			var url = "product.html";
//			if(params.hasOwnProperty("status")) {
//				url = urlAppend(url, "status=" + params.status);
//			}
//			if(params.hasOwnProperty("key")) {
//				url = urlAppend(url, "key=" + params.key);
//			}
//			pagination(url, params.page, data.body.total);
//		} else if(data.err == -1) {
//			msgErr("error");
//		}
//	}, 'json');
//};

api.programList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.program_list, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + val.title + "</td>";
				h += "<td>" + program_type[val.type] + "</td>";
				var qr = "";
				h += "<td><p>" + val.create_time + "</p><p>" + val.update_time + "</p></td>";
				free = val.is_free ? "收费" : "免费";
				h += "<td>" + free + "</td>";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.id, row);
				h += aImage(val.id, row, val.img_path);
				h += aStatus("program_status", val.id, val.status);
				h += aDele("program_del", val.id, row);
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "program.html";
			if(params.hasOwnProperty("status")) {
				url = urlAppend(url, "status=" + params.status);
			}
			if(params.hasOwnProperty("type")) {
				url = urlAppend(url, "type=" + params.type);
			}
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
			pagination(url, params.page, data.body.total);
		}else if(data.err == 2) {
			msgErr("没有符合条件的数据");
		} else {
			msgErr("error");
		}
	}, 'json');
};
api.lessonList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.lesson_list, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + val.title + "</td>";
				h += "<td>" + val.capability + "</td>";
				var qr = "";
				h += "<td><p>" + val.create_time + "</p><p>" + val.update_time + "</p></td>";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.id, row);
				h += aImage(val.id, row, val.img_path);
				h += "<a href=\"lesson_dialog.html?id=" + val.id + "\" class=\"btn btn-default\" style='color: #333' ><i class=\"fa fa-comments fa-lg\"></i></a>";
				h += aStatus("lesson_status", val.id, val.status);
				h += "<a href=\"lesson_child.html?id=" + val.id + "\" class=\"btn btn-default\" style='color: #333' ><i class=\"fa fa-book fa-lg\"></i></a>";
				//              h += "<button type=\"button\" class=\"btn btn-default\" data-toggle=\"modal\" data-target=\"#newModal\" data-ext=\"child\" data-whatever=\""+val.id+"\" data-rowid=\""+row+"\"><i class=\"fa fa-book fa-lg\"></i></button>";
				h += aDele("lesson_del", val.id, row);
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "lesson.html";
			if(params.hasOwnProperty("status")) {
				url = urlAppend(url, "status=" + params.status);
			}
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
			pagination(url, params.page, data.body.total);
		} else if(data.err == 2) {
			msgErr("没有符合条件的数据");
		}else {
			msgErr("error")
		}
	}, 'json');
};

api.productDetail = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.program_info, params, function(data) {
		if(data.err == 0) {
			$("#code").val(data.body.code);
			$("#title").val(data.body.title);
			$("#sub_title").val(data.body.sub_title);
			$("#type").val(data.body.type);
			$("#is_free").val(data.body.is_free);
			$("#video_url").val(data.body.video_url);
			$("#total").val(data.body.episode_total);
			$("#description").val(data.body.description);
		} else {
			msg("error");
		}
	}, 'json');
};

api.lessonDetail = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.lesson_info, params, function(data) {
		if(data.err == 0) {
			$("#video_id").val(data.body.video_id);
			$("#code").val(data.body.code);
			$("#title").val(data.body.title);
			$("#capability").val(data.body.capability);
			$("#is_free").val(data.body.is_free);
			$("#video_url").val(data.body.video_url);
			$("#brief").val(data.body.brief);
			$("#dialogs").val(data.body.dialogs);
		} else {
			msg("error");
		}
	}, 'json');
};

api.lessonContent = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.lesson_related, params, function(data) {
		if(data.err == 0) {
			message = Array();
			var h = "";
			row = (params.page - 1) * params.count + 1;
			data.body.forEach(function(val, i, item) {
				message[i] = val;
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + "视频" + "</td>";
				h += "<td>" + val.name + "</td>"
				h += "<td class='col-xs-2 col-lg-1'><input name='sort[" + val.id + "]' type='text' class='form-control input-sm' value=" + i + "></td><td>"
				h += "<button type=\"button\" class=\"btn btn-warning delet\" ><i class=\"fa fa-trash fa-lg\"></i></button>";
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			$(".delet").on("click", function() {
				var d = $(this).parents("tr").index();
				$(this).parents("tr").remove();
				message.splice(d, 1);
				api.lessonChildSort({
					"m": message,
					"id": id
				});
			});
		} else {
			msg("error");
		}
	}, 'json');
};

api.lessonComment = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.lesson_comments, params, function(data) {
		if(data.err == 0 && data.body != null) {
			message = Array();
			var h = "";
			row = (params.page - 1) * params.count + 1;
			data.body.forEach(function(val, i, item) {
				message[i] = val;
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + val.cue_start + "/" + val.cue_start + "</td>";
				h += "<td>" + val.transcript + "/" + val.translation + "</td>";
				h += "<td>" + val.ec_dialog_line_id + "</td><td>";
				//              h +="<td class='col-xs-2 col-lg-1'><input name='sort["+ val.id +"]' type='text' class='form-control input-sm' value="+i+"></td><td>"
				h += "<button type=\"button\" class=\"btn btn-warning delet\" ><i class=\"fa fa-trash fa-lg\"></i></button>";
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			$(".delet").on("click", function() {
				var d = $(this).parents("tr").index();
				$(this).parents("tr").remove();
				message.splice(d, 1);
				api.lessonCommentSort({
					"m": message,
					"id": id
				});
			});
		}else if(data.err == 2){
			msg("没有数据")
		} else {
			msgErr("error");
		}
	}, 'json');
};

api.lessonEdit = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.lesson_edit, params, function(data) {
		if(data.err == 0) {
			if(params.id > 0) {
				msg("更新一条记录！");
			} else {
				msg("新增一条记录！");
			}
			parent.reLoad();
		} else {
			msg("error");
		}
	}, 'json');
};
api.productEdit = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.program_edit, params, function(data) {
		if(data.err == 0) {
			if(params.id > 0) {
				msg("更新一条记录！");
			} else {
				msg("新增一条记录！");
			}
			parent.reLoad();
		} else {
			msg("error");
		}
	}, 'json');
};

api.promotionList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.promotion, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + promotion_type[val.type] + "</td>";
				h += "<td>" + val.tag + "</td>";
				h += "<td>" + promotion_grade[val.grade] + "</td>"
				h += "<td>" + val.title + "</td>";
//				h += val.img_path == null ? "<td class='col-xs-4 col-lg-2'></td>" : "<td class='col-xs-2 col-lg-2'><img class='img-thumbnail' src='" + "http://223.111.206.234:7101/src/" + val.img_path + "' /></td>";
				h += val.img_path == null ? "<td class='col-xs-4 col-lg-2'></td>" : "<td class='col-xs-4 col-lg-2'><img class='img-thumbnail' src='" + api_cfg.file_domain + val.img_path + "' /></td>";
				var qr = "";
				h += "<td><p>" + val.create_time + "</p><p>" + val.update_time + "</p></td>";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.id, row);
				h += aImage(val.id, row, val.img_path);
				h += aStatus("promotion_status", val.id, val.status);
				h += aDele("promotion_del", val.id, row);
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "promotion.html";
			if(params.hasOwnProperty("tag")) {
				url = urlAppend(url, "tag=" + params.tag);
			}
			if(params.hasOwnProperty("grade")) {
				url = urlAppend(url, "grade=" + params.grade);
			}
			url = urlAppend(url, "count=" + params.count);
			pagination(url, params.page, data.body.total);
		} else if(data.err == -1) {
			msgErr("error");
		}
	}, 'json');
};

api.promotionDetail = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.promotion, params, function(data) {
		if(data.err == 0) {
			$("#grade").val(data.body.grade);
			$("#subject").val(data.body.subject);
			$("#title").val(data.body.title);
			$("#type").val(data.body.type);
			$("#tag").val(data.body.tag);
			$("#status").val(data.body.status);
			$("#value").val(data.body.value);
		} else {
			msg("error");
		}
	}, 'json');
};

api.promotionEdit = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.promotion_edit, params, function(data) {
		if(data.err == 0) {
			if(params.id > 0) {
				msg("更新一条记录！");
			} else {
				msg("新增一条记录！");
			}
			parent.reLoad();
		} else {
			msg("error");
		}
	}, 'json');
};

api.categoryList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.category_list, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + val.id + "</th>";
				h += "<td>" + val.name + "</td>";
				var ctype = val.type == 1 ? "视频" : "课程";
				h += "<td>" + ctype + "</td>";
				h += "<td class='col-xs-2 col-lg-1'><input name='sort[" + val.id + "]' type='text' class='form-control input-sm' value='" + val.sort_num + "'/></td>";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.id, row);
				h += aImage(val.id, row, val.img_path);
				h += aStatus("category_status", val.id, val.status);
				h += aDele("category_del", val.id, row);
				h += '<a href="child_category.html?pid=' + val.id + '" class="btn btn-default" role="button" title="子栏目"><i class="fa fa-list fa-lg"></i></a>';
				var gourl = val.type == 1 ? "category_program.html?pid=":"category_lesson.html?pid="
				h += '<a href="' + gourl + val.id + '" class="btn btn-default" role="button" title="内容"><i class="fa fa-film fa-lg"></i></a>';
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "program.html";
			if(params.hasOwnProperty("status")) {
				url = urlAppend(url, "status=" + params.status);
			}
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
			//          pagination(url,params.page,data.body.total);
		} else if(data.err == -1) {
			msgErr("error");
		}
	}, 'json');
};

api.categoryDetail = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.category_list, params, function(data) {
		if(data.err == 0) {
			$("#name").val(data.body.name);
			$("#type").val(data.body.type);
			$("#status").val(data.body.status);
			$("#sort_num").val(data.body.sort_num);
			$("#pid").val(data.body.parent_id);
			$("#description").val(data.body.description);
		} else {
			msg("error");
		}
	}, 'json');
};

api.categoryEdit = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.category_edit, params, function(data) {
		if(data.err == 0) {
			if(params.id > 0) {
				msg("更新一条记录！");
			} else {
				msg("新增一条记录！");
			}
			parent.reLoad();
		} else {
			msg("error");
		}
	}, 'json');
};
api.categorysort = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.sort, opts, function(data) {
		if(data.err == 1) {
			msg("排序成功");
			reLoad();
		} else {
			msg("error");
		}
	}, "json");
};
api.categoryProgramList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.category_program, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + (val.parent_name ?  val.parent_name+"-":"") + val.name + "</td>";
				h += "<td>" + val.title +"["+ val.program_id + "]</td>";
				if(val.program_status == 1 && val.category_status == 1) {
					var status = "在线",
						clas = "btn-success";
				} else {
					var status = "下线",
						clas = "btn-default";
				}
				h += "<td><button class='btn " + clas + " btn-sm' disabled>" + status + "</button></td>";
				h += "<td class='col-xs-2 col-lg-1'><input name='sort[" + val.id + "]' type='text' class='form-control input-sm' value='" + val.sort_num + "'/></td>";
				var qr = "";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.program_id, row);
				h += aDele("category_program_del", val.id, row);
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "category_program.html";
			if(params.hasOwnProperty("status")) {
				url = urlAppend(url, "status=" + params.status);
			}
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
			if(params.hasOwnProperty("pid")) {
				url = urlAppend(url, "pid=" + params.pid);
			}
			if(params.hasOwnProperty("cid")) {
				url = urlAppend(url, "cid=" + params.cid);
			}
			pagination(url, params.page, data.body.total);
		} else if(data.err == -1) {
			msgErr("error");
		}
	}, 'json');
};

api.category_program_sort = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.program_sort, opts, function(data) {
		if(data.err == 1) {
			msg("排序成功");
			reLoad();
		} else {
			msg("error");
		}
	}, "json");
};
api.comicProgramList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.program_list, params, function(data) {
		if(data.err == 0){
			var h = "",row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\"><input type='checkbox' name=ids[] value='" + val.id + "'>&nbsp&nbsp" + row + "</th>";
				h += "<td>" + program_type[val.type] + "</td>";
				h += "<td>" + val.title + "</td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = $.url().split("?")[0];
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
			if(params.hasOwnProperty("type")) {
				url = urlAppend(url, "type=" + params.type);
			}
			pagination(url, params.page, data.body.total);
		}else if(data.err == 2){
			msgErr("没有符合条件的数据");
		}
	});
};
api.comicCateGoryList = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.category_list, params, function(data) {
		var h = '<option value="">一级栏目</option>';
		data.body.forEach(function(val, i, item) {
			if(val.type == 1) {
				h += '<option value="' + val.id + '">' + val.name + '</option>';
			}
		});
		$("#parent").html(h);
		if(params.hasOwnProperty("pid")){
			$("#parent").val(params.pid);
		}
	});
};
api.comicadd = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.comic_add, opts, function(data) {
		if(data.err == 0) {
			parent.reLoad();
		} else {
			msg("error");
		}
	});
};

api.categoryLessonList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.category_lesson, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + (val['parent_name'] ?  val['parent_name']+"-":"") + (val.name || "") + "</td>";
				h += "<td>" + val.title + "[" +val.lesson_id + "]</td>";
				if(val.lesson_status == 1 && val.category_status == 1) {
					var status = "在线",
						clas = "btn-success";
				} else {
					var status = "下线",
						clas = "btn-default";
				}
				h += "<td><button class='btn " + clas + " btn-sm' disabled>" + status + "</button></td>";
				h += "<td class='col-xs-2 col-lg-1'><input name='sort[" + val.id + "]' type='text' class='form-control input-sm' value='" + val.sort_num + "'/></td>";
				var qr = "";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.lesson_id, row);
				h += aDele("category_lesson_del", val.id, row);
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "category_lesson.html";
			if(params.hasOwnProperty("pid")) {
				url = urlAppend(url, "pid=" + params.pid);
			}
			if(params.hasOwnProperty("cid")) {
				url = urlAppend(url, "cid=" + params.cid);
			}
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
			pagination(url, params.page, data.body.total);
		} else if(data.err == 2) {
			msgErr("没有符合条件的数据");
		}else {
			msgErr("请求出错");
		}
	}, 'json');
};

api.category_lesson_sort = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.lesson_sort, opts, function(data) {
		if(data.err == 1) {
			msg("排序成功");
			reLoad();
		} else {
			msg("error");
		}
	}, "json");
};
api.categoryParentList = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.parent_category, params, function(data) {
		var h = '<option value="">一级栏目</option>';
		if(data.err == 0) {
			data.body.forEach(function(val) {
				h += '<option value="' + val.id + '">' + val.name + '</option>';
			})
		}
		$("#parent").html(h);
		if(params.hasOwnProperty("pid")) {
			$("#parent").val(params.pid);
		}
	});
};
api.categoryChildList = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.child_category, params, function(data) {
		var h = '<option value="">二级栏目</option>';
		if(data.err == 0) {
			data.body.forEach(function(val) {
				h += '<option value="' + val.id + '">' + val.name + '</option>';
			})
		}
		$("#cid").html(h);
		if(params.hasOwnProperty("cid")) {
			$("#cid").val(params.cid);
		}
	});
};
api.LessonAddList = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.lesson_list, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.list.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\"><input type='checkbox' name=ids[] value='" + val.id + "'>&nbsp&nbsp" + row + "</th>";
				h += "<td>" + val.capability + "</td>";
				h += "<td>" + val.title + "</td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "category_lesson_add.html";
			if(params.hasOwnProperty("pid")) {
				url = urlAppend(url, "pid=" + params.pid);
			}
			if(params.hasOwnProperty("cid")) {
				url = urlAppend(url, "cid=" + params.cid);
			}
			if(params.hasOwnProperty("capability")) {
				url = urlAppend(url, "capability=" + params.capability);
			}
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
			pagination(url, params.page, data.body.total);
		} else if(data.err == 2) {
			msgErr("没有符合条件的数据");
		}else {
			msgErr("error");
		}
	});
};

api.categoryLessonAdd = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.category_lesson_add, opts, function(data) {
		if(data.err == 0) {
			parent.reLoad();
		} else {
			msg("error");
		}
	});
};

api.categorychild = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.child_category, params, function(data) {
		if(data.err == 0) {
			var h = "",
				row = (params.page - 1) * params.count + 1;
			data.body.forEach(function(val, i, item) {
				h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\">" + row + "</th>";
				h += "<td>" + val.name + "</td>";
				var ctype = val.type == 1 ? "视频" : "课程";
				h += "<td>" + ctype + "</td>";
				h += "<td class='col-xs-2 col-lg-1'><input name='sort[" + val.id + "]' type='text' class='form-control input-sm' value='" + val.sort_num + "'/></td>";
				var qr = "";
				h += "<td><div class=\"btn-group\" role=\"group\">";
				h += aEdit(val.id, row);
				h += aImage(val.id, row, val.img_path);
				h += aStatus("category_status", val.id, val.status);
				h += aDele("category_del", val.id, row);
				var gourl = val.type == 1 ? "category_program.html?pid=":"category_lesson.html?pid="
				h += '<a href="'+ gourl + params.pid + '&cid=' + val.id + '" class="btn btn-default" role="button" title="内容"><i class="fa fa-film fa-lg"></i></a>';
				h += "</div></td>";
				h += "</tr>";
				row++;
			});
			$("#table-body").html(h);
			var url = "category_lesson.html";
			if(params.hasOwnProperty("pid")) {
				url = urlAppend(url, "pid=" + params.pid);
			}
			if(params.hasOwnProperty("cid")) {
				url = urlAppend(url, "cid=" + params.cid);
			}
			if(params.hasOwnProperty("key")) {
				url = urlAppend(url, "key=" + params.key);
			}
		} else if(data.err == -1) {
			msgErr("error");
		}
	}, 'json');
};

api.childcategorylist = function(opts) {
	var params = $.extend({}, api._global._pagination, opts);
	$.post(api_cfg.domain + api_cfg.program_list, params, function(data) {
		var h = "",
			row = (params.page - 1) * params.count + 1;
		data.body.list.forEach(function(val, i, item) {
			h += "<tr id=\"tr_" + val.id + "\"><th scope=\"row\" title=\"" + val.id + "\"><input type='checkbox' name=idd[" + val.id + "] value='" + val.title + "'>&nbsp&nbsp" + row + "</th>";
			h += "<td>" + val.title + "</td>";
			h += "</tr>";
			row++;
		});
		$("#table-body").html(h);
		var url = $.url().split("?")[0];
		if(params.hasOwnProperty("key")) {
			url = urlAppend(url, "key=" + params.key);
		}
		if(params.hasOwnProperty("type")) {
			url = urlAppend(url, "type=" + params.type);
		}
		pagination(url, params.page, data.body.total);
	});
};
api.lessonChildSort = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.related_sort, params, function(data) {
		if(data.err == 0) {
			msg("成功！");
			reLoad();
		} else {
			msgErr("排序失败");
		}
	}, 'json');
};
api.lessonCommentSort = function(opts) {
	var params = $.extend({}, opts);
	$.post(api_cfg.domain + api_cfg.comments_sort, params, function(data) {
		if(data.err == 0) {
			msg("成功！");
			reLoad();
		} else {
			msgErr("排序失败");
		}
	}, 'json');
};